﻿using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace MongoDBApp
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var credential = MongoCredential.CreateCredential("news", "user1", "123456789");

            var settings = new MongoClientSettings
            {
                Credential = credential,
                Server = new MongoServerAddress("192.168.6.65", 27017),
                MaxConnectionIdleTime = new TimeSpan(0, 3, 0)
            };


            var client = new MongoClient(settings);
            if (client != null)
            {
                //连接数据库
                var _database = client.GetDatabase("news");
                var _collection = _database.GetCollection<nr>("news");

                var _query = (from c in _collection.AsQueryable()
                              select c);

                foreach (var x in _query.ToList())
                {
                    Console.WriteLine("标题：" + x.title);
                    Console.WriteLine("地址：" + x.url_f);
                }

                var _q = (from c in _collection.AsQueryable()
                          where c._id == "5baca137a2158b60a496503c"
                          select c).FirstOrDefault();

                if (_q != null)
                {

                    Console.WriteLine("标题：" + _q.title);
                    Console.WriteLine("标志：" + _q.isPushed);

                    //*
                    _collection.UpdateOneAsync(
                        //要修改的记录的查询条件
                        (e => e._id == _q._id),
                        //要修改的字段Set
                        Builders<nr>
                        .Update
                        .Set(c => c.isPushed, "1")
                        .Set(c => c.meta.updatedAt, DateTime.Now)
                    );
                    //*/
                }

                Console.WriteLine("开始添加!");
                var _new = new nr()
                {
                    content = "测试测试",
                    webSite = "测试测试",
                    date = DateTime.Now,
                    isVaild = "1",
                    isPushed = "1",
                    url = "测试",
                    mainPage = "测试",
                    title = "测试",
                    meta = new m()
                    {
                        createdAt = DateTime.Now,
                        updatedAt = DateTime.MinValue
                    },
                    url_f = ""
                };
                _collection.InsertOne(_new);
                Console.WriteLine(_new._id);
            }
            else
            {
                Console.WriteLine("数据库连接错误!");
            }

            Console.ReadLine();
        }

        [Serializable]
        public class nr
        {
            [BsonRepresentation(BsonType.ObjectId)]
            public string _id { get; set; }

            public m meta { get; set; }

            public string webSite { get; set; }

            public string title { get; set; }

            [BsonDateTimeOptions(Kind = DateTimeKind.Local, Representation = BsonType.DateTime)]
            public DateTime? date { get; set; }

            public string mainPage { get; set; }

            public string url { get; set; }

            public string url_f { get; set; }

            public string content { get; set; }

            public string isPushed { get; set; }

            public string isVaild { get; set; }

            public string isVaild2 { get; set; }

            public int __v { get; set; }

        }

        [Serializable]
        public class m
        {

            [BsonDateTimeOptions(Kind = DateTimeKind.Local, Representation = BsonType.DateTime)]
            public DateTime? createdAt { get; set; }

            [BsonDateTimeOptions(Kind = DateTimeKind.Local, Representation = BsonType.DateTime)]
            public DateTime? updatedAt { get; set; }
        }
    }
}
